package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.GdsQt;
import com.corpcredit.model.Jbqk;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class GdsQtForm extends BasePage implements Serializable {
    private GenericManager<GdsQt, Long> gdsQtManager;
    private GdsQt gdsQt = new GdsQt();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setGdsQtManager(GenericManager<GdsQt, Long> gdsQtManager) {
        this.gdsQtManager = gdsQtManager;
    }

    public GdsQt getGdsQt() {
        return gdsQt;
    }

    public void setGdsQt(GdsQt gdsQt) {
        this.gdsQt = gdsQt;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        gdsQtManager.remove(gdsQt.getId());
        addMessage("gdsQt.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            gdsQt = gdsQtManager.get(id);
        } else {
            gdsQt = new GdsQt();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (gdsQt.getId() == null || gdsQt.getId() == 0);
        gdsQt.setJbqk( jbqk );
        gdsQt.setUsername(getRequest().getRemoteUser());
        gdsQt.setOptdate(new Date());
        gdsQt.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        
        gdsQtManager.save(gdsQt);

        String key = (isNew) ? "gdsQt.added" : "gdsQt.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	this.id = gdsQt.getId();
            return "edit";
        }
    }
} 